﻿//
// $HeadURL: http://codeka-memcache.googlecode.com/svn/trunk/Codeka.Memcache/Commands/GetServerInfoCommand.cs $
// $Revision: 9 $
// $Author: dean.codeka $     $Date: 2010-02-21 00:55:37 +0000 (Sun, 21 Feb 2010) $
//
// Copyright © 2009-2010 Dean Harding, all rights reserved.
//

using System;
using System.Collections.Generic;
using System.Text;

namespace Codeka.Memcache.Commands
{
    /// <summary>
    /// This command gets information about the server we're connected to
    /// (e.g. it's version number, stats and so on).
    /// </summary>
    public class GetServerInfoCommand : Command
    {
        /// <summary>
        /// Constructs a new <see cref="GetServerInfoCommand"/>.
        /// </summary>
        public GetServerInfoCommand()
        {
            ServerInfo = new ServerInfo();
        }

        /// <summary>
        /// Gets the <see cref="ServerInfo"/> that we actually got from the server.
        /// </summary>
        public ServerInfo ServerInfo { get; private set; }

        /// <summary>
        /// Get an <see cref="IEnumerable{T}"/> collection of <see cref="Packet"/>s that
        /// we want to send to the server.
        /// </summary>
        public override IEnumerable<Packet> GetPackets()
        {
            yield return new Packet {
                    Header = {
                            PacketType = PacketType.Request,
                            OpCode = PacketOpCode.Version,
                        },
                };
        }

        /// <summary>
        /// This is called when we get a response from the server.
        /// </summary>
        public override bool ProcessResponse(Packet pkt)
        {
            if (pkt.Header.OpCode == PacketOpCode.Version)
            {
                ServerInfo.ServerVersion = Encoding.ASCII.GetString(pkt.Value);
            }

            return false;
        }
    }
}
